使用JDBC type4连接DB2

Categories: Database; Tagged with: ; @ October 2nd, 2010 22:57

1. 首先要在工程中加入DB2连接文件db2jcc.jar, 该文件可从DB2安装目录中找到.
2. 建立连接:

	public void setUp() throws Exception {
		Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
		Properties connProperties = new Properties();
		connProperties.put("user", "ADMINISTRATOR");
		connProperties.put("password", "liguoliang.com");

		connection = DriverManager.getConnection(db2.getJdbcUrl("192.168.1.113", 50000, "meta"), connProperties);
	}

3. 执行SQL语句举例:

	public void validateConnection(Connection connection) throws SQLException {
		if(connection == null) {
			throw new SQLException("Connection is null");
		}

		Statement stmt = null;
		ResultSet rs = null;

		try {
			stmt = connection.createStatement();
			rs = stmt.executeQuery("VALUES(1)"); // 该语句将返回1
			rs.next();
			if(rs.getInt(1) != 1) {
				throw new SQLException("SQL Exception: values(1) != 1");
			}
		}finally {
			JDBCUtilities.close(rs);
		}
	}

有用的链接:

JDBC连接DB2的一些总结

用java访问数据库DB2代码的实际操作  http://database.51cto.com/art/201008/216908.htm

使用 JDBC 连接不同版本 DB2 数据库的兼容性问题 http://www.ibm.com/developerworks/cn/data/library/techarticles/0402chenjunwei/0402chenjunwei.html#author1

<->



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.